<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>《Flutter—Overlay用法》 - 飞雪无情的博客</title><meta name="Description" content="专注于IT互联网，包括但不限于Go语言(golang)、Java、Android、Python、项目管理、抖音分析、软件架构等"><meta property="og:title" content="《Flutter—Overlay用法》" />
<meta property="og:description" content="
本文主要介绍Flutter Overlay 用法简介。
" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://www.flysnow.org/posts/flutter/flutterOverlay%E7%94%A8%E6%B3%95/" />
<meta property="article:published_time" content="2021-07-31T18:23:24+08:00" />
<meta property="article:modified_time" content="2021-07-31T18:23:24+08:00" />
<meta name="twitter:card" content="summary"/>
<meta name="twitter:title" content="《Flutter—Overlay用法》"/>
<meta name="twitter:description" content="
本文主要介绍Flutter Overlay 用法简介。
"/>
<meta name="application-name" content="飞雪无情的博客">
<meta name="apple-mobile-web-app-title" content="飞雪无情的博客"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://www.flysnow.org/posts/flutter/flutterOverlay%E7%94%A8%E6%B3%95/" /><link rel="prev" href="https://www.flysnow.org/posts/flutter/flutterState%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/" /><link rel="next" href="https://www.flysnow.org/posts/flutter/flutter%E6%98%AF%E5%A6%82%E4%BD%95%E5%B7%A5%E4%BD%9C%E7%9A%84/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "《Flutter—Overlay用法》",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/www.flysnow.org\/posts\/flutter\/flutterOverlay%E7%94%A8%E6%B3%95\/"
        },"genre": "posts","keywords": "flutter","wordcount":  312 ,
        "url": "https:\/\/www.flysnow.org\/posts\/flutter\/flutterOverlay%E7%94%A8%E6%B3%95\/","datePublished": "2021-07-31T18:23:24+08:00","dateModified": "2021-07-31T18:23:24+08:00","publisher": {
            "@type": "Organization",
            "name": "飞雪无情"},"author": {
                "@type": "Person",
                "name": "飞雪无情"
            },"description": ""
    }
    </script></head>
    <body header-desktop="" header-mobile=""><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : '' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><div class="logo-wrapper">
  <a href="/%20/" class="logo">飞雪无情的博客</a>
</div>

<nav class="site-navbar">
  <ul id="menu" class="menu">
    <li class="menu-item">
      <a class="menu-item-link" href="/tools/">工具</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/archives/">归档</a>
    </li><li class="menu-item">
      <a class="menu-item-link" href="/about/">关于</a>
    </li>
  </ul>
</nav><main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">Contents</h2>
            <div class="toc-content" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">《Flutter—Overlay用法》</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>飞雪无情</a></span>&nbsp;<span class="post-category">included in <a href="/categories/Flutter/"><i class="far fa-folder fa-fw"></i>Flutter</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2021-07-31">2021-07-31</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;312 words&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;One minute&nbsp;</div>
        </div><div class="featured-image"><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        data-srcset="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 1.5x, https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 2x"
        data-sizes="auto"
        alt="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png"
        title="https://luckly007.oss-cn-beijing.aliyuncs.com/img/90c6cc12-742e-4c9f-b318-b912f163b8d0.png" /></div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>Contents</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li><a href="#指定位置">指定位置</a></li>
    <li><a href="#跟随滚动">跟随滚动</a></li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><blockquote>
<p>本文主要介绍Flutter Overlay 用法简介。</p>
</blockquote>
<h1 id="overlay-基本用法">Overlay 基本用法</h1>
<p><code>Overlay</code> 实际上是一个 <code>Stack</code>，所以 <code>OverlayEntry</code> 的内容可以是 <code>Positioned</code>。</p>
<pre><code>var overlayEntry = OverlayEntry(builder: (context) =&gt; Positioned(...););
Overlay.of(context).insert(overlayEntry);
</code></pre><ul>
<li>显示 Overlay - 使用 <code>Overlay.of(context).insert()</code> 方法</li>
<li>隐藏 Overlay - 使用 <code>overlayEntry.remove()</code> 方法</li>
</ul>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaybasic.gif"
        data-srcset="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaybasic.gif, https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaybasic.gif 1.5x, https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaybasic.gif 2x"
        data-sizes="auto"
        alt="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaybasic.gif"
        title="overlay-basic" /></p>
<h1 id="overlay-高级用法">Overlay 高级用法</h1>
<h2 id="指定位置">指定位置</h2>
<p>有时我们想指定 Overlay 的显示位置，实现方式如下：</p>
<ul>
<li>先使用 <code>BuildContext.findRenderObject()</code> 来找到当前 Widget 对应的 RenderObject</li>
<li>再使用 <code>RenderBox.localToGlobal()</code> 找到 Widget 在屏幕上的位置</li>
</ul>
<p>具体见 <code>_calcPos()</code> 方法。</p>
<pre><code>class _MyButton extends StatefulWidget {
  final PosCallback callback;

  const _MyButton({Key key, this.callback}) : super(key: key);

  @override
  __MyButtonState createState() =&gt; __MyButtonState();
}

class __MyButtonState extends State&lt;_MyButton&gt; {
  List&lt;double&gt; _calcPos() {
    RenderBox renderBox = context.findRenderObject();
    var size = renderBox.size;
    var offset = renderBox.localToGlobal(Offset.zero);

    return [offset.dx, offset.dy + size.height];
  }

  @override
  Widget build(BuildContext context) {
    return RaisedButton(
      onPressed: () {
        List&lt;double&gt; list = _calcPos();
        widget.callback(list[0], list[1]);
      },
      child: Text('show overlay'),
    );
  }
}
</code></pre><p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaypos.gif"
        data-srcset="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaypos.gif, https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaypos.gif 1.5x, https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaypos.gif 2x"
        data-sizes="auto"
        alt="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlaypos.gif"
        title="overlay-pos" /></p>
<h2 id="跟随滚动">跟随滚动</h2>
<p>使用 <a href="https://api.flutter.dev/flutter/rendering/LayerLink-class.html" target="_blank" rel="noopener noreffer">LayerLink</a> 来让 Overlay 跟随指定的 Widget 来滚动。</p>
<ul>
<li>LayerLink 用于联系 <a href="https://api.flutter.dev/flutter/rendering/LeaderLayer-class.html" target="_blank" rel="noopener noreffer">LeaderLayer</a> 和 <a href="https://api.flutter.dev/flutter/rendering/FollowerLayer-class.html" target="_blank" rel="noopener noreffer">FollowerLayer</a></li>
<li>使用 <a href="https://api.flutter.dev/flutter/widgets/CompositedTransformTarget-class.html" target="_blank" rel="noopener noreffer">CompositedTransformTarget</a> 来创建 LeaderLayer</li>
<li>使用 <a href="https://api.flutter.dev/flutter/widgets/CompositedTransformFollower-class.html" target="_blank" rel="noopener noreffer">CompositedTransformFollower</a> 来创建 FollowLayer</li>
</ul>
<p>代码篇幅较长，这里就不贴上来了。完整代码见 <a href="https://gist.github.com/410063005/4034c395f3305feeb262482a96d4278e" target="_blank" rel="noopener noreffer">gist</a>。</p>
<p><img
        class="lazyload"
        src="/svg/loading.min.svg"
        data-src="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlayscroll.gif"
        data-srcset="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlayscroll.gif, https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlayscroll.gif 1.5x, https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlayscroll.gif 2x"
        data-sizes="auto"
        alt="https://blog-1251688504.cos.ap-shanghai.myqcloud.com/2020/07/15/overlayscroll.gif"
        title="overlay-scroll" /></p>
<h1 id="参考">参考</h1>
<ul>
<li><a href="https://medium.com/saugo360/https-medium-com-saugo360-flutter-using-overlay-to-display-floating-widgets-2e6d0e8decb9" target="_blank" rel="noopener noreffer">Flutter: Using Overlay to display floating widgets | by AbdulRahman AlHamali | SAUGO 360 | Medium</a></li>
<li><a href="https://api.flutter.dev/flutter/rendering/LayerLink-class.html" target="_blank" rel="noopener noreffer">LayerLink class - rendering library - Dart API</a></li>
</ul></div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>Updated on 2021-07-31</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"></div>
            <div class="post-info-share">
                <span></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/flutter/">flutter</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">Back</a></span>&nbsp;|&nbsp;<span><a href="/">Home</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/posts/flutter/flutterState%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/" class="prev" rel="prev" title="《Flutter—State生命周期》"><i class="fas fa-angle-left fa-fw"></i>《Flutter—State生命周期》</a>
            <a href="/posts/flutter/flutter%E6%98%AF%E5%A6%82%E4%BD%95%E5%B7%A5%E4%BD%9C%E7%9A%84/" class="next" rel="next" title="《Flutter是如何工作的》">《Flutter是如何工作的》<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
</article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">Powered by <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.79.1">Hugo</a> | Theme - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">飞雪无情</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="Back to Top">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="View Comments">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"Copy to clipboard","maxShownLines":10},"comment":{}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
